package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcAlarmLightLocationMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcAlarmLightLocation;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcRfidReaderGroupAlarmLight;
import com.csun.cmny.provider.model.dto.alarmlight.AlarmLightDto;
import com.csun.cmny.provider.model.dto.alarmlight.AlarmLightQueryDto;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectDeviceDataDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.AlarmLightVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcAlarmLightLocationService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcRfidReaderGroupAlarmLightService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-22 15:03
 * @Description: the class dmc alarm light location service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcAlarmLightLocationServiceImpl extends BaseService<DmcAlarmLightLocation>
        implements DmcAlarmLightLocationService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcAlarmLightLocationMapper dmcAlarmLightLocationMapper;
    @Resource
    private DmcRfidReaderGroupAlarmLightService dmcRfidReaderGroupAlarmLightService;

    /**
     * add alarm light
     *
     * @param alarmLightDto the alarm light dto
     * @param loginAuthDto  the login auth dto
     * @return the int.
     */
    @Override
    public Integer addAlarmLight(AlarmLightDto alarmLightDto, LoginAuthDto loginAuthDto) {

        // 添加设备
        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(alarmLightDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.ALARM_LIGHT.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, loginAuthDto);
        if (result < 1) {
            new DmcBizException(ErrorCodeEnum.DMC10062001);
        }

        // 绑定报警灯和位置
        DmcAlarmLightLocation dmcAlarmLightLocation = new DmcAlarmLightLocation();
        dmcAlarmLightLocation.setDeviceId(dmcDevice.getId());
        dmcAlarmLightLocation.setLocation(alarmLightDto.getLocation());
        result += dmcAlarmLightLocationMapper.insertSelective(dmcAlarmLightLocation);

        return result;
    }

    /**
     * edit alarm light
     *
     * @param alarmLightDto the alarm light dto
     * @param loginAuthDto  the login auth dto
     * @return the int.
     */
    @Override
    public Integer editAlarmLight(AlarmLightDto alarmLightDto, LoginAuthDto loginAuthDto) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(alarmLightDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.ALARM_LIGHT.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, loginAuthDto);
        if (result < 1) {
            new DmcBizException(ErrorCodeEnum.DMC10062002);
        }

        // 绑定报警灯和位置
        DmcAlarmLightLocation dmcAlarmLightLocation = new DmcAlarmLightLocation();
        dmcAlarmLightLocation.setDeviceId(dmcDevice.getId());
        dmcAlarmLightLocation.setLocation(alarmLightDto.getLocation());
        result += dmcAlarmLightLocationMapper.updateByPrimaryKeySelective(dmcAlarmLightLocation);

        return result;
    }

    /**
     * delete alarm light by id
     *
     * @param id the alarm light id
     * @return the int.
     */
    @Override
    public Integer deleteAlarmLightById(Long id) {

        // 判断是否已经绑定门禁组
        DmcRfidReaderGroupAlarmLight dmcRfidReaderGroupAlarmLight = new DmcRfidReaderGroupAlarmLight();
        dmcRfidReaderGroupAlarmLight.setAlarmLightId(id);
        int count = dmcRfidReaderGroupAlarmLightService.selectCount(dmcRfidReaderGroupAlarmLight);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC10062003);
        }

        Integer result = dmcDeviceService.deleteDmcDeviceById(id);
        result += dmcAlarmLightLocationMapper.deleteByPrimaryKey(id);

        return result;
    }

    /**
     * query alarm light list with page
     *
     * @param alarmLightQueryDto the alarm light query dto
     *
     * @return the list.
     */
    @Override
    public List<AlarmLightVo> queryAlarmLightListWithPage(AlarmLightQueryDto alarmLightQueryDto) {

        PageHelper.startPage(alarmLightQueryDto.getPageNum(), alarmLightQueryDto.getPageSize());

        return dmcAlarmLightLocationMapper.queryAlarmLightListWithPage(alarmLightQueryDto);
    }

    /**
     * select one alarm light by rfid read group id
     *
     * @return the alarm light vo
     */
    @Override
    public AlarmLightVo selectOneByRfidReadGroupId(Long rfidReaderGroupId) {

        AlarmLightQueryDto alarmLightQueryDto = new AlarmLightQueryDto();
        alarmLightQueryDto.setRfidReaderGroupId(rfidReaderGroupId);

        PageHelper.startPage(1, 1);

        List<AlarmLightVo> alarmLightVoList = dmcAlarmLightLocationMapper
                .queryAlarmLightListWithPage(alarmLightQueryDto);

        if (alarmLightVoList == null || alarmLightVoList.size() < 1) {
            return null;
        }

        return alarmLightVoList.get(0);
    }

    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return dmcAlarmLightLocationMapper.selectDeviceData(selectDeviceDataDto);
    }
}
